package com.itlong.cloud.controller.finance;

import com.itlong.cloud.POJO.DTO.operate.OperateShareProfitRuleDTO;
import com.itlong.cloud.POJO.VO.MessageVO;
import com.itlong.cloud.POJO.VO.operate.OperateShareProfitRuleGetVO;
import com.itlong.cloud.abstracts.BaseController;
import com.itlong.cloud.annotation.AuthAccessRequired;
import com.itlong.cloud.annotation.LoggerInfo;
import com.itlong.cloud.constants.LogDescConstant;
import com.itlong.cloud.enumerate.ecode.BaseErrorCodeEnum;
import com.itlong.cloud.enumerate.ecode.OperateErrorCodeEnum;
import com.itlong.cloud.paymenthub.IOperateShareProfitRuleServiceClient;
import com.itlong.cloud.thrown.DataAccessException;
import com.itlong.cloud.utils.network.http.HttpProtocolUtil;
import com.itlong.cloud.utils.page.Page;
import com.itlong.cloud.utils.words.NumberHandlerUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * <desc>
 *      分润规则信息管理控制器
 * <desc/>
 *
 * @createDate 2019/05/20
 */
@RestController
@RequestMapping("/propertyValue/shareProfitRule")
@RefreshScope
public class OperateShareProfitRuleController extends BaseController {

    @Autowired
    private IOperateShareProfitRuleServiceClient iOperateShareProfitRuleServiceClient;

    /**
     * <desc>
     *      分页获取分润规则信息
     * </desc>
     *
     * @param operateShareProfitRuleDTO 分润规则信息DTO
     * @return 标准格式化响应结果
     * @author wangzhi
     * @createDate 2019/05/20
     */
    @ResponseBody
    @AuthAccessRequired
    @PostMapping("getPage")
    public Object getPage(OperateShareProfitRuleDTO operateShareProfitRuleDTO) {
        try {
            HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            operateShareProfitRuleDTO= HttpProtocolUtil.parseRequestParamToDTO(OperateShareProfitRuleDTO.class,request);
            if (operateShareProfitRuleDTO == null) {
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            Page<OperateShareProfitRuleGetVO> page = iOperateShareProfitRuleServiceClient.getPage(operateShareProfitRuleDTO);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(), page);
        } catch (Exception e) {
            throw new DataAccessException("【运营-物业增值-分润规则getPage】分页获取分润规则信息失败", e);
        }
    }

    /**
     * <desc>
     *      添加分润规则信息
     * </desc>
     *
     * @param operateShareProfitRuleDTO 分润规则信息DTO
     * @return 标准格式化响应结果
     * @author wangzhi
     * @createDate 2019/05/20
     */
    @ResponseBody
    @PostMapping(path = "save")
    @AuthAccessRequired
    @LoggerInfo(operateDesc = LogDescConstant.PROFIT_RULE_SAVE_LOG_DESC)
    public Object save(OperateShareProfitRuleDTO operateShareProfitRuleDTO){
        try {
            HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            operateShareProfitRuleDTO= HttpProtocolUtil.parseRequestParamToDTO(OperateShareProfitRuleDTO.class,request);
            if (operateShareProfitRuleDTO == null || StringUtils.isBlank(operateShareProfitRuleDTO.getRuleName()) /*|| StringUtils.isBlank(operateShareProfitRuleDTO.getProfitPercent())*/
                    || StringUtils.isBlank(operateShareProfitRuleDTO.getPlatformPercent()) || StringUtils.isBlank(operateShareProfitRuleDTO.getMyUserName())) {
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            if (!NumberHandlerUtil.isInRange(operateShareProfitRuleDTO.getType(), 1, 2)) {
                return new MessageVO(BaseErrorCodeEnum.PARAM_ERR.getErrorCode());
            }
            try {
//                if (Float.parseFloat(operateShareProfitRuleDTO.getProfitPercent()) > 100.0f) {
//                    return new MessageVO(BaseErrorCodeEnum.PARAM_ERR.getErrorCode());
//                }
                if (Float.parseFloat(operateShareProfitRuleDTO.getPlatformPercent()) > 100.0f) {
                    return new MessageVO(BaseErrorCodeEnum.PARAM_ERR.getErrorCode());
                }
                operateShareProfitRuleDTO.setPropertyPercent(100.0f - Float.parseFloat(operateShareProfitRuleDTO.getPlatformPercent()) + "");
            } catch (NumberFormatException e) {
                return new MessageVO(BaseErrorCodeEnum.PARAM_ERR.getErrorCode());
            }
            iOperateShareProfitRuleServiceClient.save(operateShareProfitRuleDTO);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode());
        } catch (Exception e) {
            throw new DataAccessException("【运营-物业增值-分润规则save】添加分润规则信息失败", e);
        }
    }

    /**
     * <desc>
     *      查询单个分润规则
     * </desc>
     *
     * @param operateShareProfitRuleDTO 分润规则信息DTO
     * @return 标准格式化响应结果
     * @author wangzhi
     * @createDate 2019/05/20
     */
    @ResponseBody
    @PostMapping(path = "getOne")
    public Object getOne(OperateShareProfitRuleDTO operateShareProfitRuleDTO){
        try {
            HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            operateShareProfitRuleDTO= HttpProtocolUtil.parseRequestParamToDTO(OperateShareProfitRuleDTO.class,request);
            if (operateShareProfitRuleDTO == null || StringUtils.isBlank(operateShareProfitRuleDTO.getRuleId())) {
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            OperateShareProfitRuleGetVO operateShareProfitRuleGetVO = iOperateShareProfitRuleServiceClient.getOne(operateShareProfitRuleDTO);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(), operateShareProfitRuleGetVO);
        } catch (Exception e) {
            throw new DataAccessException("【运营-物业增值-分润规则getOne】查询单个分润规则信息失败", e);
        }
    }

    /**
     * <desc>
     *      修改分润规则信息
     * </desc>
     *
     * @param operateShareProfitRuleDTO 分润规则信息DTO
     * @return 标准格式化响应结果
     * @author wangzhi
     * @createDate 2019/05/20
     */
    @ResponseBody
    @PostMapping(path = "update")
    @AuthAccessRequired
    @LoggerInfo(operateDesc = LogDescConstant.PROFIT_RULE_UPDATE_LOG_DESC)
    public Object update(OperateShareProfitRuleDTO operateShareProfitRuleDTO){
        try {
            HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            operateShareProfitRuleDTO= HttpProtocolUtil.parseRequestParamToDTO(OperateShareProfitRuleDTO.class,request);
            if (operateShareProfitRuleDTO == null || StringUtils.isBlank(operateShareProfitRuleDTO.getRuleName()) /*|| StringUtils.isBlank(operateShareProfitRuleDTO.getProfitPercent())*/
                    || StringUtils.isBlank(operateShareProfitRuleDTO.getPlatformPercent()) || StringUtils.isBlank(operateShareProfitRuleDTO.getRuleId())
                    || StringUtils.isBlank(operateShareProfitRuleDTO.getMyUserName())) {
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            if (!iOperateShareProfitRuleServiceClient.checkRuleIdle(operateShareProfitRuleDTO.getRuleId())) {
                return new MessageVO(OperateErrorCodeEnum.SHARE_PROFIT_RULE_IN_USE_ERR.getErrorCode());
            }
            if (!NumberHandlerUtil.isInRange(operateShareProfitRuleDTO.getType(), 1, 2)) {
                return new MessageVO(BaseErrorCodeEnum.PARAM_ERR.getErrorCode());
            }
            try {
//                if (Float.parseFloat(operateShareProfitRuleDTO.getProfitPercent()) > 100.0f) {
//                    return new MessageVO(BaseErrorCodeEnum.PARAM_ERR.getErrorCode());
//                }
                if (Float.parseFloat(operateShareProfitRuleDTO.getPlatformPercent()) > 100.0f) {
                    return new MessageVO(BaseErrorCodeEnum.PARAM_ERR.getErrorCode());
                }
                operateShareProfitRuleDTO.setPropertyPercent(100.0f - Float.parseFloat(operateShareProfitRuleDTO.getPlatformPercent()) + "");
            } catch (NumberFormatException e) {
                return new MessageVO(BaseErrorCodeEnum.PARAM_ERR.getErrorCode());
            }
            iOperateShareProfitRuleServiceClient.update(operateShareProfitRuleDTO);
            return new MessageVO(operateShareProfitRuleDTO.getRuleId(), BaseErrorCodeEnum.SUCCESS.getErrorCode());
        } catch (Exception e) {
            throw new DataAccessException("【运营-物业增值-分润规则update】修改分润规则信息失败", e);
        }
    }

    /**
     * <desc>
     *      删除分润规则信息，可批量
     * </desc>
     *
     * @param ruleIds 分润规则信息id，多个英文逗号相连
     * @return 标准格式化响应结果
     * @author wangzhi
     * @createDate 2019/05/20
     */
    @ResponseBody
    @PostMapping(path = "delete")
    @AuthAccessRequired
    @LoggerInfo(operateDesc = LogDescConstant.PROFIT_RULE_DELETE_LOG_DESC)
    public Object delete(String ruleIds){
        try {
            HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            ruleIds=request.getAttribute("ruleIds")==null?null:request.getAttribute("ruleIds").toString();
            if (StringUtils.isBlank(ruleIds)) {
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            if (!iOperateShareProfitRuleServiceClient.checkRuleIdle(ruleIds)) {
                return new MessageVO(OperateErrorCodeEnum.SHARE_PROFIT_RULE_IN_USE_ERR.getErrorCode());
            }
            iOperateShareProfitRuleServiceClient.delete(ruleIds);
            return new MessageVO(ruleIds, BaseErrorCodeEnum.SUCCESS.getErrorCode());
        } catch (Exception e) {
            throw new DataAccessException("【运营-物业增值-分润规则delete】删除分润规则信息失败", e);
        }
    }

}
